package com.Service.impl;

import com.Service.ListingService;
import com.dao.ListingMapper;
import com.model.pojo.Listing;
import com.model.pojo.Portfolio;
import com.util.GetNumber;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.beans.Transient;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class listingServiceImpl implements ListingService {
    @Resource
    public ListingMapper listingMapper;


    @Override
    public List<Listing> listOf(String card, String diseaseName) {

        String str = null;
        String serialNumber = null;
        int big = 0;
        int id = 1;
        String card1 = card;
        long time = System.currentTimeMillis();
        Date date = new Date(time);
//流水号开始
        String ma = "yyyyMMddHHmmss";

        SimpleDateFormat forma = new SimpleDateFormat(ma);

        String nwdate = forma.format(date);
        System.out.println("1");
        List<Listing> list = null;
//        listingMapper.search(card);
        if (card != "" && diseaseName != "") {
            if (listingMapper.search(card) == null) {
                card = null;
                System.out.println("2");
                list = listingMapper.search(card);

                if (list.size() == 0) {
                    System.out.println("3");
                    big = 0;

                } else {
//                id = list.get(list.size() - 1).getID() + 1;

                    str = String.valueOf(list.get(list.size() - 1).getListingSerialNumber());

                    str.substring(0, 8);
                    int num = Integer.valueOf(str.substring(0, 8));

                    if (num > Integer.valueOf(GetNumber.today())) {
                        big = 0;

                    } else if (num == Integer.valueOf(GetNumber.today())) {
                        for (int i = 0; i < list.size(); i++) {
                            str = String.valueOf(list.get(i).getListingSerialNumber());


                            if (GetNumber.today().equals(str.substring(0, 8))) {


                                int small = Integer.valueOf(str.substring(14));

                                if (big < small) {
                                    big = small;

                                } else if (big > small) {
                                    big = big;

                                }


                            }
                        }

                    }


                }
            } else {
                System.out.println(big + "else");
                boolean bl = false;
                String num = "";
                int today = Integer.valueOf(GetNumber.today());
                list = listingMapper.search(card);
                for (int i = 0; i < list.size(); i++) {

                    num = list.get(i).getListingSerialNumber().substring(0, 8);

                    if (today == Integer.valueOf(num)) {

                        bl = true;

                        num = list.get(i).getListingSerialNumber();

                        break;

                    } else {
                        System.out.println(8);
                        bl = false;
                    }

                }

                if (bl == true) {

                    for (int i = 0; i < list.size(); i++) {
                        str = String.valueOf(list.get(i).getListingSerialNumber());


                        if (GetNumber.today().equals(str.substring(0, 8))) {


                            int small = Integer.valueOf(str.substring(14));

                            if (big < small) {
                                big = small;

                            } else if (big > small) {
                                big = big;

                            }


                        } else {

                            big = 0;

                        }
                    }
                    big = big - 1;

                    System.out.println(9);
                    System.out.println(big);
                } else {
                    card = null;
                    list = listingMapper.search(card);

                    if (list.size() == 0) {
                        big = 1;


                    } else {


                        for (int i = 0; i < list.size(); i++) {
                            str = String.valueOf(list.get(i).getListingSerialNumber());


                            if (GetNumber.today().equals(str.substring(0, 8))) {
                                System.out.println(12);

                                int small = Integer.valueOf(str.substring(14));

                                if (big < small) {
                                    big = small;

                                } else if (big > small) {
                                    big = big;

                                }

                                System.out.println(13);
                            } else {

                                big = 0;

                            }
                        }

                    }
                }


            }
            if (list.size() == 0) {
                id = 1;
            } else {
                list = listingMapper.search("");
                id = list.get(list.size() - 1).getID() + 1;
            }

            serialNumber = nwdate + GetNumber.zero(6, big + 1);
            //流水号结束

            //费用价格查询开始
            Portfolio portfolio = listingMapper.diseasePrice(diseaseName);

            card = card1;
            boolean bl=listingMapper.listOf(id, serialNumber, card, portfolio.getPortfolioName(), portfolio.getPortfolioPrice(), "未缴费");
            if(bl==true){
                list = listingMapper.listMessage();

            }else {
                list=null;

            }

        } else {
            list = listingMapper.listMessage();
        }


        return list;


    }

    @Override
    public List<Listing> personalMessage(String listingSerialNumber) {
        List<Listing> list = listingMapper.personalMessage(listingSerialNumber);

        return list;
    }


}
